package apis

import (
	"context"
	"fmt"
	"net/http"
	"time"
	"vrcm/src/vrcm/common/apis"

	"github.com/gin-gonic/gin"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"

	"vrcm/src/vrcm/app/admin/service/domian"
	ccenginDto "vrcm/src/vrcm/app/admin/service/dto/ccengine"
	"vrcm/src/vrcm/app/admin/service/dto/domian"
)

type DomainConfig struct {
	api.Api
}

// GetPage 获取DomianConfig列表
// @Summary 获取DomianConfig列表
// @Description 获取DomianConfig列表
// @Tags DomainConfig
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]models.DomainConfig}} "{"code": 200, "data": [...]}"
// @Router /api/v1/domian-config [get]
// @Security Bearer
func (e DomainConfig) GetPage(c *gin.Context) {
	req := dto.DomainConfigGetPageReq{}
	s := service.DomainConfig{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	var count int64

	data, err := s.GetPage(&req, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取加速域名失败， %s", err.Error()))
		return
	}

	e.PageOK(data, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 获取DomainConfig
// @Summary 获取DomainConfig
// @Description 获取DomainConfig
// @Tags DomainConfig
// @Param id path int false "id"
// @Success 200 {object} response.Response{data=models.DomainConfig} "{"code": 200, "data": [...]}"
// @Router /api/v1/domain-config/{id} [get]
// @Security Bearer
func (e DomainConfig) Get(c *gin.Context) {
	req := dto.DomainConfigGetReq{}
	s := service.DomainConfig{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	tx := e.Orm.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()
	if err := tx.Error; err != nil {
		return
	}

	data, err := s.Get(&req, tx)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取加速域名配置失败， %s", err.Error()))
		return
	}
	tx.Commit()

	e.OK(data, "查询成功")
}

// Insert 创建DomianConfig
// @Summary 创建DomianConfig
// @Description 创建DomianConfig
// @Tags DomainConfig
// @Accept application/json
// @Product application/json
// @Param data body dto.DomainConfigInsertReq true "data"
// @Success 200 {object} response.Response	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/domian-config [post]
// @Security Bearer
func (e DomainConfig) Insert(c *gin.Context) {
	// 设置超时时间为5秒
	ctx, cancel := context.WithTimeout(c.Request.Context(), 60*time.Second)
	defer cancel()

	// 在这里执行需要设置超时的操作
	select {
	default:
		req := dto.DomainConfigInsertReq{}
		s := service.DomainConfig{}
		err := e.MakeContext(c).
			MakeOrm().
			Bind(&req).
			MakeService(&s.Service).
			Errors
		if err != nil {
			e.Logger.Error(err)
			e.Error(apis.InternalError.Code, err, err.Error())
			return
		}
		err = s.Insert(&req)
		if err != nil {
			if err.Error() == string(apis.ParamterValidatorError.Code) {
				e.Error(apis.ParamterValidatorError.Code, err, fmt.Sprintf("创建DomianConfig失败，priority取值范围为1-2"))
			} else {
				e.Error(500, err, fmt.Sprintf("创建DomianConfig失败，\r\n失败信息 %s", err.Error()))
			}

			return
		}

		e.OK(req.GetId(), "创建成功")
	case <-ctx.Done():
		err := ctx.Err()
		e.Logger.Trace("请求超时:", err)
		c.JSON(http.StatusRequestTimeout, gin.H{"error": "请求超时"})
	}

}

// Update 修改DomianConfig
// @Summary 修改DomianConfig
// @Description 修改DomianConfig
// @Tags DomainConfig
// @Accept application/json
// @Product application/json
// @Param id path int true "id"
// @Param data body dto.DomianConfigUpdateReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/domian-config/{id} [put]
// @Security Bearer
func (e DomainConfig) Update(c *gin.Context) {
	req := dto.DomainConfigUpdateReq{}
	s := service.DomainConfig{}
	// 设置超时时间为5秒
	ctx, cancel := context.WithTimeout(c.Request.Context(), 60*time.Second)
	defer cancel()

	// 在这里执行需要设置超时的操作
	select {
	default:
		err := e.MakeContext(c).
			MakeOrm().
			Bind(&req).
			MakeService(&s.Service).
			Errors
		if err != nil {
			e.Logger.Error(err)
			e.Error(apis.InternalError.Code, err, err.Error())
			return
		}

		err = s.Update(&req)
		if err != nil {
			if err.Error() == string(apis.ParamterValidatorError.Code) {
				e.Error(apis.ParamterValidatorError.Code, err, fmt.Sprintf("创建DomianConfig失败，priority取值范围为1-2"))
			} else {
				e.Error(500, err, fmt.Sprintf("修改域名失败， %s", err.Error()))
			}
			return
		}

		e.OK(req.Id, "修改域名成功")
	case <-ctx.Done():
		err := ctx.Err()
		e.Logger.Trace("请求超时:", err)
		c.JSON(http.StatusRequestTimeout, gin.H{"error": "请求超时"})
	}

}

func (e DomainConfig) UpdateDomain(c *gin.Context) {
	req := dto.DomainConfigUpdateReq{}
	s := service.DomainConfig{}
	// 设置超时时间为5秒
	ctx, cancel := context.WithTimeout(c.Request.Context(), 60*time.Second)
	defer cancel()

	// 在这里执行需要设置超时的操作
	select {
	default:
		err := e.MakeContext(c).
			MakeOrm().
			Bind(&req).
			MakeService(&s.Service).
			Errors
		if err != nil {
			e.Logger.Error(err)
			e.Error(apis.InternalError.Code, err, err.Error())
			return
		}

		err = s.UpdateDomain(&req)
		if err != nil {
			if err.Error() == string(apis.OriginTimeOutError.Code) {
				e.Error(apis.OriginTimeOutError.Code, err, fmt.Sprintf("修改域名失败， %s", "超时时间范围为5-60秒"))
			} else if err.Error() == string(apis.ParamterValidatorError.Code) {
				e.Error(apis.ParamterValidatorError.Code, err, fmt.Sprintf("修改域名失败，priority取值范围为1-2"))
			} else {
				e.Error(500, err, fmt.Sprintf("修改域名失败， %s", err.Error()))
			}

			return
		}
		e.OK(req.Id, "修改域名成功")
	case <-ctx.Done():
		err := ctx.Err()
		e.Logger.Trace("请求超时:", err)
		c.JSON(http.StatusRequestTimeout, gin.H{"error": "请求超时"})
	}

}

// Delete 删除DomianConfig
// @Summary 删除DomianConfig
// @Description 删除DomianConfig
// @Tags DomainConfig
// @Param data body dto.DomianConfigDeleteReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/domian-config [delete]
// @Security Bearer
func (e DomainConfig) Delete(c *gin.Context) {
	s := service.DomainConfig{}
	req := dto.DomainConfigDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.Remove(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除加速域名失败， %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "删除成功")
}

func (e DomainConfig) SaveBackSourceConfig(c *gin.Context) {
	s := service.DomainConfig{}
	req := dto.BackSourceConfigSaveReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.SaveBackSourceConfig(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("保存回源配置失败 %s", err.Error()))
		return
	}
	e.OK(nil, "保存回源配置成功")
}

func (e DomainConfig) SaveSourceStationConfig(c *gin.Context) {
	s := service.DomainConfig{}
	req := dto.SourceStationConfigSaveReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.SaveSourceStationConfig(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("保存源站配置失败 %s", err.Error()))
		return
	}
	e.OK(req.Id, "保存源站配置成功")
}

func (e DomainConfig) DeleteSourceStationConfig(c *gin.Context) {
	s := service.DomainConfig{}
	req := dto.SourceStationConfigDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.DeleteSourceStationConfig(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除源站配置失败 %s", err.Error()))
		return
	}
	e.OK(req.Id, "删除源站配置成功")
}

func (e DomainConfig) SaveCacheExpireConfig(c *gin.Context) {
	s := service.DomainConfig{}
	req := dto.CacheExpireConfigSaveReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.SaveCacheExpireConfig(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("保存缓存过期配置失败 %s", err.Error()))
		return
	}
	e.OK(req.Id, "保存缓存过期成功")
}

func (e DomainConfig) SaveStateCodeExpireConfig(c *gin.Context) {
	s := service.DomainConfig{}
	req := dto.StateCodeExpireConfigSaveReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.SaveStateCodeExpireConfig(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("保存状态码过期配置失败 %s", err.Error()))
		return
	}
	e.OK(req.Id, "保存状态码过期成功")
}

func (e DomainConfig) SaveMultiStateCodeExpireConfig(c *gin.Context) {
	s := service.DomainConfig{}
	req := dto.MultiStateCodeExpireConfigSaveReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	tx := e.Orm.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()
	if err = tx.Error; err != nil {
		e.Error(500, err, fmt.Sprintf("保存状态码过期配置失败 %s", err.Error()))
		return
	}
	err = s.SaveMultiStateCodeExpireConfig(tx, &req, false)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("保存状态码过期配置失败 %s", err.Error()))
		return
	}
	tx.Commit()
	e.OK("", "保存状态码过期成功")
}

func (e DomainConfig) DeleteCacheExpireConfig(c *gin.Context) {
	s := service.DomainConfig{}
	req := dto.CacheExpireConfigDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.DeleteCacheExpireConfig(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除缓存过期配置失败 %s", err.Error()))
		return
	}
	e.OK(req.Id, "删除缓存过期配置成功")
}

func (e DomainConfig) SaveMultiCacheExpireConfig(c *gin.Context) {
	s := service.DomainConfig{}
	req := dto.MultiCacheExpireConfigSaveReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	tx := e.Orm.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()
	if err = tx.Error; err != nil {
		e.Error(500, err, fmt.Sprintf("保存缓存参数配置失败 %s", err.Error()))
		return
	}
	err = s.SaveMultiCacheExpireConfig(tx, &req, false)
	if err != nil {
		tx.Rollback()
		e.Error(500, err, fmt.Sprintf("保存缓存参数配置失败 %s", err.Error()))
		return
	}
	tx.Commit()
	e.OK("", "保存缓存参数成功")
}

func (e DomainConfig) DeleteStateCodeExpireConfig(c *gin.Context) {
	s := service.DomainConfig{}
	req := dto.StateCodeExpireConfigDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.DeleteStateCodeExpireConfig(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除状态码过期配置失败 %s", err.Error()))
		return
	}
	e.OK(req.Id, "删除状态码过期配置成功")
}

func (e DomainConfig) SaveCacheFilterConfig(c *gin.Context) {
	s := service.DomainConfig{}
	req := dto.CacheFilterConfigSaveReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.SaveCacheFilterConfig(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("保存过滤参数配置失败 %s", err.Error()))
		return
	}
	e.OK(req.Id, "保存过滤参数成功")
}

func (e DomainConfig) SaveMultiCacheFilterConfig(c *gin.Context) {
	s := service.DomainConfig{}
	req := dto.MultiCacheFilterConfigSaveReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	tx := e.Orm.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()
	if err = tx.Error; err != nil {
		e.Error(500, err, fmt.Sprintf("保存过滤参数配置失败 %s", err.Error()))
		return
	}

	err = s.SaveMultiCacheFilterConfig(tx, &req, false)
	if err != nil {
		tx.Rollback()
		e.Error(500, err, fmt.Sprintf("保存过滤参数配置失败 %s", err.Error()))
		return
	}
	tx.Commit()
	e.OK("", "保存过滤参数成功")
}

func (e DomainConfig) DeleteCacheFilterConfig(c *gin.Context) {
	s := service.DomainConfig{}
	req := dto.CacheFilterConfigDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.DeleteCacheFilterConfig(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除过滤参数配置失败 %s", err.Error()))
		return
	}
	e.OK(req.Id, "删除过滤参数配置成功")
}

func (e DomainConfig) SaveDistributionProtocol(c *gin.Context) {
	s := service.DomainConfig{}
	req := dto.DistributionProtocolSaveReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	tx := e.Orm.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()
	if err = tx.Error; err != nil {
		e.Error(500, err, fmt.Sprintf("保存分发协议配置失败 %s", err.Error()))
		return
	}
	err = s.SaveDistributionProtocol(tx, &req, false)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("保存分发协议配置失败 %s", err.Error()))
		return
	}
	tx.Commit()
	e.OK(req.Id, "保存分发协议成功")
}

func (e DomainConfig) DeleteDistributionProtocol(c *gin.Context) {
	s := service.DomainConfig{}
	req := dto.DistributionProtocolDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.DeleteDistributionProtocol(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除分发协议配置失败 %s", err.Error()))
		return
	}
	e.OK(req.Id, "删除分发协议配置成功")
}

func (e DomainConfig) StartDomain(c *gin.Context) {
	s := service.DomainConfig{}
	req := dto.StartDomainReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.StartDomain(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("启用加速域名失败 %s", err.Error()))
		return
	}
	e.OK(req.Id, "启用加速域名成功")
}

func (e DomainConfig) StopDomain(c *gin.Context) {
	s := service.DomainConfig{}
	req := dto.StopDomainReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.StopDomain(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("停用加速域名失败 %s", err.Error()))
		return
	}
	e.OK(req.Id, "停用加速域名成功")
}

func (e DomainConfig) RecordUpOrDown(c *gin.Context) {
	s := service.DomainConfig{}
	req := dto.UpOrDownReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.CacheRecordUpOrDown(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("上移或者下移失败 %s", err.Error()))
		return
	}
	e.OK(req.Id, "上移或者下移成功")
}

func (e DomainConfig) DomainOwnerVerify(c *gin.Context) {
	s := service.DomainConfig{}
	req := ccenginDto.VerifyReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.DomainOwnerVerify(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("域名认证失败 %s", err.Error()))
		return
	}
	e.OK(data, "域名认证失败成功")
}

func (e DomainConfig) DomainOwnerVerify2(c *gin.Context) {
	s := service.DomainConfig{}
	req := ccenginDto.VerifyReq2{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.DomainOwnerVerify2(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("域名认证失败 %s", err.Error()))
		return
	}
	e.OK(data, "域名认证成功")
}

func (e DomainConfig) SaveAntiTheftConfig(c *gin.Context) {
	s := service.DomainConfig{}
	req := dto.AntiTheftConfigSaveReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	tx := e.Orm.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()
	if err = tx.Error; err != nil {
		e.Error(500, err, fmt.Sprintf("保存防盗链配置失败 %s", err.Error()))
	}
	err = s.SaveAntiTheftConfig(tx, &req, false)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("保存防盗链配置失败 %s", err.Error()))
		return
	}
	tx.Commit()
	e.OK(req.Id, "保存防盗链配置成功")
}

func (e DomainConfig) SaveUrlSign(c *gin.Context) {
	s := service.DomainConfig{}
	req := dto.UrlSignSaveReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	tx := e.Orm.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()
	if err = tx.Error; err != nil {
		e.Error(500, err, fmt.Sprintf("保存url鉴权配置失败 %s", err.Error()))
		return
	}
	err = s.SaveUrlSign(tx, &req, false)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("保存url鉴权配置失败 %s", err.Error()))
		return
	}
	tx.Commit()
	e.OK(req.Id, "保存url鉴权配置成功")
}

func (e DomainConfig) SaveOverWriteUrl(c *gin.Context) {
	s := service.DomainConfig{}
	req := ccenginDto.OriginUrlRewriteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.SaveOverWriteUrl(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("保存重写地址失败 %s", err.Error()))
		return
	}
	e.OK(req.Id, "保存重写地址成功")
}

func (e DomainConfig) GetOverWriteUrl(c *gin.Context) {
	s := service.DomainConfig{}
	req := ccenginDto.GetOriginUrlRewriteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.GetOverWriteUrl(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取重写地址失败 %s", err.Error()))
		return
	}
	e.OK(req.Id, "获取重写地址成功")
}

func (e DomainConfig) DeleteOverWriteUrl(c *gin.Context) {
	s := service.DomainConfig{}
	req := ccenginDto.DeleteUrlRewriteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.DeleteOverWriteUrl(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除重写地址失败 %s", err.Error()))
		return
	}
	e.OK(nil, "删除重写地址成功")
}

func (e DomainConfig) MoveDown(c *gin.Context) {
	s := service.DomainConfig{}
	req := ccenginDto.MoveDown{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.MoveView(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("上移下移失败 %s", err.Error()))
		return
	}
	e.OK(nil, "上移下移成功")
}

func (e DomainConfig) GetDomain(c *gin.Context) {
	s := service.DomainConfig{}
	req := dto.GetByDomainReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetDomain(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取域名失败 %s", err.Error()))
		return
	}
	e.OK(data, "获取域名成功")
}

func (e DomainConfig) SaveMultiHeaderConfig(c *gin.Context) {
	s := service.DomainConfig{}
	req := dto.MultiHeaderConfigSaveReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	tx := e.Orm.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()
	if err = tx.Error; err != nil {
		e.Error(500, err, fmt.Sprintf("保存header配置失败 %s", err.Error()))
		return
	}
	err = s.SaveMultiHeaderConfig(tx, &req, false)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("保存header配置失败 %s", err.Error()))
		return
	}
	tx.Commit()
	e.OK(nil, "保存header配置成功")
}

func (e DomainConfig) SaveCompressConfig(c *gin.Context) {
	s := service.DomainConfig{}
	req := dto.CompressConfigSaveReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	tx := e.Orm.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()
	if err = tx.Error; err != nil {
		e.Error(500, err, fmt.Sprintf("保存压缩配置失败 %s", err.Error()))
		return
	}
	err = s.SaveCompressConfig(tx, &req, false)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("保存压缩配置失败 %s", err.Error()))
		return
	}
	tx.Commit()
	e.OK(nil, "保存压缩配置成功")
}

func (e DomainConfig) GetIpInfo(c *gin.Context) {
	s := service.DomainConfig{}
	req := ccenginDto.GetIpInfoReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetIpInfo(req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("获取ip信息失败 %s", err.Error()))
		return
	}
	e.OK(data, "获取ip信息成功")
}
